//
//  SpecialViewController.m
//  小胖兔旅行指南
//
//  Created by 小胖兔 on 15/4/10.
//  Copyright (c) 2015年 小胖兔. All rights reserved.
//

#import "SpecialViewController.h"
#import "SpecialTableViewCell.h"
#import "UIScrollView+PullLoad.h"
#import "SDetailsViewController.h"
#import "AFNetworkTool.h"

#define SpecialUrl @"http://tubu.ibuzhai.com/rest/v1/trail/types"
@interface SpecialViewController ()<UITableViewDelegate, UITableViewDataSource, PullDelegate>

@end

@implementation SpecialViewController

- (void)dealloc
{
    [_tableView release];
    [_tableArray release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setHead];
    [self createTableView];
    [self requestData];
    
}

#pragma mark -
#pragma 设置导航栏
- (void)setHead
{
    UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen] bounds].size.width, 44)];
    [aLabel setText:@"路线专题"];
    [aLabel setTextAlignment:NSTextAlignmentCenter];
    [aLabel setTextColor:[UIColor whiteColor]];
    [self.navigationItem setTitleView:aLabel];

}

#pragma mark -
#pragma 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self.tableView setBackgroundColor:[UIColor colorWithRed:231.0 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1.0]];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setPullDelegate:self];
    [self.tableView setCanPullDown:YES];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView registerClass:[SpecialTableViewCell class] forCellReuseIdentifier:@"specialCell"];
    
    
}

#pragma mark - 
#pragma 下拉刷新
- (void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        [self performSelector:@selector(downLoad) withObject:nil afterDelay:0];
    }
}

- (void)downLoad
{
    [self.tableView reloadData];
    [self.tableView stopLoadWithState:PullDownLoadState];
}






#pragma mark -
#pragma 请求数据
- (void)requestData
{
    SpecialViewController *my = self;
    NSDictionary *dic = @{@"app_version": @"1.9.0", @"device_type": @"1"};
    [AFNetworkTool JSONDataWithUrl:SpecialUrl parameter:dic success:^(id json) {
        my.tableArray = [json objectForKey:@"types"];
        [my.tableView reloadData];
    } fail:^{
        nil;
    }];
}




#pragma mark -
#pragma tableView协议方法实现
// cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableArray count];
}
// cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ([[UIScreen mainScreen] bounds].size.width - 10) / 239.89 * 134.76 + 10;
//    return 215.041477;
}

// 创建cell 赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"specialCell"];
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"xiaopangtu" ofType:@"jpg"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage imageWithData:data];
    [cell.MyimageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"url"]] placeholderImage:image];
    
    
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SDetailsViewController *sDetails = [[SDetailsViewController alloc] init];
    [sDetails setOneID:[[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
    [sDetails setOneName:[[self.tableArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    [sDetails setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:sDetails animated:YES];
    [sDetails release];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
